define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'weike/forum/index' + location.search,
                    add_url: 'weike/forum/add',
                    edit_url: 'weike/forum/edit',
                    auth:'user/enterprise/auth',
                    del_url: 'weike/forum/del',
                    multi_url: 'weike/forum/multi',
                    import_url: 'weike/forum/import',
                    table: 'weike_forum',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user.username', title: __('User.username'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'user.avatar', title: __('User.avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user_id', title: __('User_id'), visible: false},
                        {field: 'top_id', title: __('Top_id'), visible: false},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'two_id', title: __('Two_id'), visible: false},
                        {field: 'content', title: __('发布内容'),
                            formatter : function(value, row, index, field){
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + row.content + "'>" + value + "</span>";
                            },
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"150px"
                                    }
                                };
                            }},


                        {field: 'is_shop', title: __('Is_shop'), searchList: {"是":__('是'),"否":__('否')}, formatter: Table.api.formatter.normal},
                        {field: 'like', title: __('Like')},
                        {field: 'evaluate', title: __('Evaluate')},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [

                                {
                                    name: 'parent',
                                    text: __('查看商品'),
                                    icon: '',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'weike/forumgoods?weike_forum_id={id}',
                                    visible:function(row){//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if(row.is_shop== '是'){
                                            return true;//显示
                                        }else{
                                            return false;//隐藏
                                        }
                                    },
                                    refresh:true
                                },
                                {
                                    name: 'parent',
                                    text: __('查看评论'),
                                    icon: '',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'weike/forumevaluate?weike_forum_id={id}',
                                    visible:function(row){//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if(row.evaluate>0){
                                            return true;//显示
                                        }else{
                                            return false;//隐藏
                                        }
                                    },
                                    refresh:true
                                },


                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },


        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
